import axios from 'axios'
import store from '../store'
import iview from 'iview'
import {api} from '../config'

axios.defaults.baseURL = api.base

axios.interceptors.request.use((config) => {
  config.headers.Authorization = `Bearer ${store.state.access_token}`;
  return config
})
axios.interceptors.response.use((response) => {
    if (response.data) {
        return response.data;
    } else {
        return response;
    }
}, (error) => {
    if (error.response.data) {
        console.log(error.response.data)
        iview.Notice.error({
            message: '提示',
            desc: error.response.data.message,
        })
        if(error.response.status == 401) {
            store.dispatch('clear')
        }
        return error.response.data;
    } else {
        return error
    }
})
export default axios